//
//  KSZoneItem.swift
//  ANOA
//
//  Created by 邱仙凯 on 2024/11/21.
//  Copyright © 2024 zwb. All rights reserved.
//
 
import UIKit
import Kingfisher
 
class KSZoneItem: KSFormHItem {
    var zoneSegV:ZoneSegView = {
       let v = ZoneSegView()
        return v
    }()
    func setCollecDataSource(arr:[SegmenttData]) {
        zoneSegV.setCollecDataSource(arr: arr)
    }
    convenience init(appearance: KSFormBaseAppear? = KSFormBaseAppear()) {
        self.init(title: "", appearance: appearance)
       
        self.textView.isHidden = true
        self.addSubview(zoneSegV)
        zoneSegV.collecSelected = { idx,data in
            if let clo = self.collecSelected {
                clo(idx,data)
            }
        }
        zoneSegV.snp.makeConstraints { (make) in
             make.right.equalTo(self)
             make.left.equalTo(self)
             make.top.equalTo(self)
             make.bottom.equalTo(self)
        
        }
       
     }
    var collecSelected:((Int,SegmenttData)->())?
   
}

enum ZoneShowType {
    // 居左，宽度用计算的
    // 全部平铺，平均宽度
    case left,all
}
enum ZoneBgType {
    // 底部UI
    // 拱门UI
    case bottomLine,roundBg
}

class ZoneSegView:UIView,UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout {
    lazy var collectionView:UICollectionView = {
        // 1.创建layout
         let layout = UICollectionViewFlowLayout()
        layout.itemSize = CGSize(width: CollecWidth - 0.5, height: 35)
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        layout.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
        layout.scrollDirection = .horizontal
        // 2.创建UICollectionView
        let v = UICollectionView(frame: CGRect.zero, collectionViewLayout: layout)
        v.backgroundColor = .white
        v.showsHorizontalScrollIndicator = false
        v.register(KSZoneItemCell.self, forCellWithReuseIdentifier: KSZoneItemCell.identifier)
 
        return v
    }()
    var blockxColor = Theme.primaryColor()
    var dataArray = [SegmenttData]()
    var maxlengths = [Int]()
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = .white
        collectionView.dataSource = self
        collectionView.delegate = self
        collectionView.backgroundColor = .clear
        self.addSubview(collectionView)
        collectionView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }

        
    }
    var collecSelected:((Int,SegmenttData)->())?

    var selectIndx = 0 {
        didSet {
            self.collectionView.reloadData()
        }
    }
    var zShowType:ZoneShowType = .left
    var bgType:ZoneBgType = .bottomLine {
        didSet {
            if bgType == .bottomLine {
                self.backgroundColor = .white

            } else {
                
                self.backgroundColor = Theme.commonBg
            }
        }
    }
    var blockWidthMultipliedBy = 0.85

    func setCollecDataSource(arr:[SegmenttData]) {
        self.dataArray = arr
        var widths = [Int]()
        if zShowType == .left {
            let nmArr = arr.compactMap{$0.nm}
            for itemx in  nmArr {
                let w = 33 + itemx.count * 15
                widths.append(w)

            }
        } else {
            var sigleW = 0
            if arr.count > 0 {
                sigleW = Int(self.ks_width)/arr.count
            }
            
            widths = Array(repeating: sigleW, count: arr.count)
        }
        
        maxlengths = widths
        
        
        self.collectionView.reloadData()
    }
 
 
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return self.dataArray.count
 
    }
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        
        
        let ww = indexPath.row
        let height = collectionView.ks_height
        return CGSize(width: maxlengths[ww], height: Int(height))
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: KSZoneItemCell.identifier, for: indexPath as IndexPath) as! KSZoneItemCell
        cell.blockWidthMultipliedBy = self.blockWidthMultipliedBy
        let one = self.dataArray[indexPath.row]
        cell.bgType = self.bgType
        cell.blockx.backgroundColor = blockxColor
        cell.title.text = one.nm
        cell.setSelect(indexPath.row == selectIndx)
        
 
         return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        self.selectIndx = indexPath.row
        self.collectionView.reloadData()
        
        if let clo = self.collecSelected {
            clo(indexPath.row,self.dataArray[indexPath.row])
        }
        
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}



class KSZoneItemCell: BadgeCollectionViewCell {
    var bg:UIView = {
        var v = UIView()
        v.backgroundColor = .white
        let lefttop = CornerView(width: 10, outColor: Theme.commonBg, innerColor: .white, type: .leftTop)
        v.addSubview(lefttop)
        lefttop.snp.makeConstraints { make in
            make.left.top.equalToSuperview()
             make.width.height.equalTo(10)
        }
        let righttop = CornerView(width: 10, outColor: Theme.commonBg, innerColor: .white, type: .rightTop)
        v.addSubview(righttop)
        righttop.snp.makeConstraints { make in
            make.right.top.equalToSuperview()
             make.width.height.equalTo(10)
        }
        return v
    }()
    var blockWidthMultipliedBy = 0.85 {
        didSet {
            blockx.snp.remakeConstraints { (make) in
                make.centerX.equalTo(self.contentView)
                make.bottom.equalTo(self.contentView).offset(-1)
                make.width.equalToSuperview().multipliedBy(blockWidthMultipliedBy)
                make.height.equalTo(6)
          
            }
        }
    }

    var blockx: UIView = {
        let v = UIView()
        v.backgroundColor = Theme.primaryColor()
        return v
    }()
    var title: UILabel = {
        let lab :UILabel = UILabel()
        lab.textColor = UIColor(kshex:"#222222")
        lab.font = UIFont.systemFont(ofSize: 15)
        lab.textAlignment = .center
        lab.numberOfLines = 2
        return lab
    }()
 
    var bgType:ZoneBgType = .bottomLine {
        didSet {
            self.bg.isHidden = bgType == .bottomLine
        }
    }
    func setSelect(_ flag:Bool) {
        if flag {
            title.font = UIFont.boldSystemFont(ofSize: 19)
            title.textColor = UIColor.black
            blockx.isHidden = false
            title.snp.remakeConstraints { (make) in
                make.center.equalTo(self.contentView)
            }
        } else {
            title.font = UIFont.systemFont(ofSize: 15)
            title.textColor = UIColor(kshex: "#8991B0")
            blockx.isHidden = true
            title.snp.remakeConstraints { (make) in
                make.centerX.equalTo(self.contentView)
                make.centerY.equalTo(self.contentView).offset(5)
            }
        }
    }

    convenience required init(coder : NSCoder){
        self.init()
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        self.initUtil()
    }
    
    func initUtil() {
        self.backgroundColor = .clear
        self.contentView.backgroundColor = .clear
        self.contentView.addSubview(bg)
        self.contentView.addSubview(title)
        self.contentView.addSubview(blockx)
        bg.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(3)
            make.right.equalToSuperview().offset(-3)
            make.top.equalToSuperview().offset(8)
            make.bottom.equalToSuperview()

        }
        blockx.snp.makeConstraints { (make) in
            make.centerX.equalTo(self.contentView)
            make.bottom.equalTo(self.contentView).offset(-1)
            make.width.equalToSuperview().multipliedBy(blockWidthMultipliedBy)
            make.height.equalTo(6)
      
        }
        
        title.snp.makeConstraints { (make) in
            make.center.equalTo(self.contentView)
      
        }
        
        
    }
}
